/**
 * 根据数组创建一个反向的单向链表
 * 
 */

type LinkType = {
    value:number,
    next?:LinkType
}


const reverseLinkList = (listNode:LinkType)=>{
    let preNode:LinkType | undefined = undefined
    let curNode:LinkType | undefined = undefined
    let nextNode:LinkType = listNode

    while(nextNode){
        if(curNode && !preNode){
            //@ts-ignore
            delete curNode.next
        }
        //反转指针
        if(preNode && curNode){
            //@ts-ignore
            curNode.next = preNode
        }
        //移动指针
        preNode = curNode
        curNode = nextNode
        //@ts-ignore
        nextNode = nextNode?.next
    }
    curNode!.next = preNode
    return curNode
}

const createLinkList = (arr:number[]):LinkType=>{
    const length = arr.length
    let curNode:LinkType = {
        value:arr[length - 1]
    }
    if(length === 1) return curNode
    for(let i = length -2;i >= 0;i-- ){
        curNode = {
            value:arr[i],
            next:curNode
        }
    }
    return curNode
}


const arr = createLinkList([1,2,3,4,5])
console.log('arr',arr)
const reversearr = reverseLinkList(arr)

console.log('reverseLinkList',reversearr)



/** 
 * 
 * 反转单向链表 
 */